package zjh_20230328;

import zjh_20230310.Collection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//未完成。。。
public class ConnectionPool {
    private  ArrayList<Connection> connections;
    //连接池
    private String url;
    private String user;
    private String password;
    private int size;
    public ConnectionPool(int size,String url,String user,String password) throws SQLException {
        this.connections = new ArrayList<>(size);
        this.size = size;
        this.url = url;
        this.user = user;
        this.password = password;
        //添加代理
        for (int i = 0; i < size; i++) {
            Connection connection = DriverManager.getConnection(url, user, password);
            connections.add(connection);
        }
    }
    public Connection getConnection() throws SQLException {
        if (connections.size() > 0){
            return connections.remove(connections.size() - 1);
        }
        else {
//            return createConnection(url,user,password);
            return null;
        }
    }

//    private Connection createConnection(String url, String user, String password) throws SQLException {
//        Connection connection = DriverManager.getConnection(url, user, password);
//    }

}
